<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 检查是否存在满足条件的数字组合（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给定一个正整数数组&#xff0c;检查数组中是否存在满足规则的数字组合</p> 
<p>规则&#xff1a;<strong>A &#61; B &#43; 2C</strong></p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行输出数组的元素个数。</p> 
<p>接下来一行输出所有数组元素&#xff0c;用空格隔开。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>如果存在满足要求的数&#xff0c;在同一行里依次输出规则里A/B/C的取值&#xff0c;用空格隔开。</p> 
<p>如果不存在&#xff0c;输出0。</p> 
<p></p> 
<h4>备注</h4> 
<ol><li>数组长度在3-100之间。</li><li>数组成员为0-65535&#xff0c;数组成员可以重复&#xff0c;但每个成员只能在结果算式中使用一次。如&#xff1a;数组成员为[0, 0, 1, 5]&#xff0c;0出现2次是允许的&#xff0c;但结果0 &#61; 0 &#43; 2 * 0是不允许的&#xff0c;因为算式中使用了3个0。</li><li>用例保证每组数字里最多只有一组符合要求的解。</li></ol> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:69px;">输入</td><td style="width:429px;"> <p>4<br /> 2 7 3 0</p> </td></tr><tr><td style="width:69px;">输出</td><td style="width:429px;">7 3 2</td></tr><tr><td style="width:69px;">说明</td><td style="width:429px;">7 &#61; 3 &#43; 2 * 2</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:70px;">输入</td><td style="width:428px;">3<br /> 1 1 1</td></tr><tr><td style="width:70px;">输出</td><td style="width:428px;">0</td></tr><tr><td style="width:70px;">说明</td><td style="width:428px;">找不到满足条件的组合</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题数组长度不大&#xff0c;只有3~100&#xff0c;因此三重for暴力破解&#xff0c;也不会超时。</p> 
<p>我们可以先将数组降序&#xff0c;则第一层for选择的数必然是最大的&#xff0c;可以作为A&#xff0c;第二层和第三层是较小数&#xff0c;可以作为B和C&#xff0c;或者C和B。</p> 
<p>备注中还说明了</p> 
<blockquote> 
 <p>用例保证每组数字里最多只有一组符合要求的解。</p> 
</blockquote> 
<p>因此&#xff0c;我们一旦找到符合要求的解&#xff0c;就可以直接return</p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; Integer.parseInt(sc.nextLine());
    Integer[] arr &#61;
        Arrays.stream(sc.nextLine().split(&#34; &#34;)).map(Integer::parseInt).toArray(Integer[]::new);

    System.out.println(getResult(n, arr));
  }

  public static String getResult(int n, Integer[] arr) {
    Arrays.sort(arr, (a, b) -&gt; b - a);

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      for (int j &#61; i &#43; 1; j &lt; n; j&#43;&#43;) {
        for (int k &#61; j &#43; 1; k &lt; n; k&#43;&#43;) {
          if (arr[i] &#61;&#61; arr[j] &#43; 2 * arr[k]) {
            return arr[i] &#43; &#34; &#34; &#43; arr[j] &#43; &#34; &#34; &#43; arr[k];
          }
          if (arr[i] &#61;&#61; arr[k] &#43; 2 * arr[j]) {
            return arr[i] &#43; &#34; &#34; &#43; arr[k] &#43; &#34; &#34; &#43; arr[j];
          }
        }
      }
    }

    return &#34;0&#34;;
  }
}
</code></pre> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    let n &#61; parseInt(lines[0]);
    let arr &#61; lines[1].split(&#34; &#34;).map(Number);

    console.log(getResult(n, arr));

    lines.length &#61; 0;
  }
});

function getResult(n, arr) {
  arr.sort((a, b) &#61;&gt; b - a);

  for (let i &#61; 0; i &lt; n; i&#43;&#43;) {
    for (let j &#61; i &#43; 1; j &lt; n; j&#43;&#43;) {
      for (let k &#61; j &#43; 1; k &lt; n; k&#43;&#43;) {
        if (arr[i] &#61;&#61; arr[j] &#43; 2 * arr[k]) {
          return &#96;${arr[i]} ${arr[j]} ${arr[k]}&#96;;
        }

        if (arr[i] &#61;&#61; arr[k] &#43; 2 * arr[j]) {
          return &#96;${arr[i]} ${arr[k]} ${arr[j]}&#96;;
        }
      }
    }
  }

  return &#34;0&#34;;
}
</code></pre> 
<p></p> 
<h4 style="background-color:transparent;">Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())
arr &#61; list(map(int, input().split()))


# 算法入口
def getResult():
    arr.sort(reverse&#61;True)

    for i in range(n):
        for j in range(i &#43; 1, n):
            for k in range(j &#43; 1, n):
                if arr[i] &#61;&#61; arr[j] &#43; 2 * arr[k]:
                    return f&#34;{arr[i]} {arr[j]} {arr[k]}&#34;

                if arr[i] &#61;&#61; arr[k] &#43; 2 * arr[j]:
                    return f&#34;{arr[i]} {arr[k]} {arr[j]}&#34;

    return &#34;0&#34;


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int cmp(const void* a, const void* b) {
    return (*(int*) b) - (*(int*) a);
}

int main() {
    int n;
    scanf(&#34;%d&#34;, &amp;n);

    int nums[n];
    for(int i&#61;0; i&lt;n; i&#43;&#43;) {
        scanf(&#34;%d&#34;, &amp;nums[i]);
    }

    qsort(nums, n, sizeof(int), cmp);

    for(int i&#61;0; i&lt;n; i&#43;&#43;) {
        for(int j&#61;i&#43;1; j&lt;n; j&#43;&#43;) {
            for(int k&#61;j&#43;1; k&lt;n; k&#43;&#43;) {
                if(nums[i] &#61;&#61; nums[j] &#43; 2 * nums[k]) {
                    printf(&#34;%d %d %d\n&#34;, nums[i], nums[j], nums[k]);
                    return 0;
                }

                if(nums[i] &#61;&#61; nums[k] &#43; 2 * nums[j]) {
                    printf(&#34;%d %d %d\n&#34;, nums[i], nums[k], nums[j]);
                    return 0;
                }
            }
        }
    }

    puts(&#34;0&#34;);

    return 0;
}</code></pre> 
<p> </p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>